package com.hb.trans.utils;

import android.content.Context;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class DBUtils
{

    /**
     * apk目录下的数据库路径
     */
    public static final String DB_PATH = "data/data/com.hb.trans/databases/";

    /**
     * db文件名
     */
    public static final String DB_NAME = "user";

    /**
     * 将raw目录下的db，拷贝到apk安装路径的数据库目录下
     *
     * @param //context上下文
     * @param //copyRawDbResId将要拷贝的raw资源ID
     * @param apkDbPath APK数据库路径
     * @param dbName 数据库文件名
     * @param refresh 是否覆盖之前的db文件
     * @return 拷贝是否成功
     * @throws //IOException
     */
    public static boolean copyRawDBToApkDb(Context context, int copyRawDbResId, String apkDbPath, String dbName,boolean refresh)
        throws IOException
    {
        boolean b = false;

        File f = new File(apkDbPath);
        if (!f.exists())
        {
            f.mkdirs();
        }

        File dbFile = new File(apkDbPath + dbName);
        b = isDbFileExists(dbFile,refresh);
        if (!b)
        {
            InputStream is = context.getResources().openRawResource(copyRawDbResId);

            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry entry;

            while ((entry = zis.getNextEntry()) != null)
            {
                int size;
                byte[] buffer = new byte[1024 * 2];

                OutputStream fos = new FileOutputStream(apkDbPath + entry.getName());
                BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

                while ((size = zis.read(buffer, 0, buffer.length)) != -1)
                {
                    bos.write(buffer, 0, size);
                }
                bos.flush();
                bos.close();
            }
            zis.close();
            is.close();
        }
        return !b;
    }



    public static void copyDBToDatabases(Context context) {
        try {
            String outFileName = DB_PATH + DB_NAME;
            File file = new File(DB_PATH);
            if (!file.mkdirs()) {
                file.mkdirs();
            }
            File dataFile = new File(outFileName);
            if (dataFile.exists()) {
                dataFile.delete();
            }
            InputStream myInput;
            myInput = context.getApplicationContext().getAssets().open(DB_NAME);
            OutputStream myOutput = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (IOException e) {
//            Log.i(TAG, "error--->" + e.toString());
            e.printStackTrace();
        }

    }

    /**
     * 检查DB文件是否存在
     * @param f 文件名
     * @param refresh 是否覆盖之前的db文件
     * @return
     */
    private static boolean isDbFileExists(File f,boolean refresh)
    {
        boolean b = false;
        if (f.exists())
        {
            if (refresh)
            {
                f.delete();
                b = false;
            }
            else
            {
                b = true;
            }
        }
        return b;
    }
}
